﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Kinect_MediaPlayer
{
	class WMP
	{
		private string Pcommand;
		private bool isOpen;
		private Form1 form;

		[DllImport("winmm.dll")]
		private static extern int mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, int bla);

		[System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint = "mciGetErrorString")]
		public static extern bool mciGetErrorString(int dwError, StringBuilder lpstrBuffer, int uLength);

		/// <SUMMARY>
		/// Not much to construct here
		/// </SUMMARY>
		public WMP(Form1 form)
		{
			this.form = form;
		}

		/// <SUMMARY>
		/// Stops currently playing audio file
		/// </SUMMARY>
		public void Close()
		{
			Pcommand = "close MediaFile";
			mciSendString(Pcommand, null, 0, 0);
			isOpen = false;
		}

		/// <SUMMARY>
		/// Opens audio file to play
		/// </SUMMARY>
		/// <param name="sFileName" />This is the audio file's path and filename</param />
		public void Open(string sFileName)
		{
			StringBuilder buffer = new StringBuilder(128);
			
			Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
			int err = mciSendString(Pcommand, buffer, 0, 0);

            if (err != 0)
            {
                
                bool returnValue = mciGetErrorString(err, buffer, buffer.Capacity);
                string error = buffer.ToString();
                MessageBox.Show("There was an error opening the file: \n" + error);
            }
            else
            {
                isOpen = true;
            }
		}

		/// <SUMMARY>
		/// Plays selected audio file
		/// </SUMMARY>
		/// <param name="loop" />If True,audio file will repeat</param />
		public void Play(bool loop)
		{
			if (isOpen)
			{
				StringBuilder buffer = new StringBuilder(128);

				Pcommand = "play MediaFile";
				if (loop) Pcommand += " REPEAT";
				int err = mciSendString(Pcommand, null, 0, 0);

				bool returnValue = mciGetErrorString(err, buffer, buffer.Capacity);
				string error = buffer.ToString();
			}
		}

		public void OpenVideo(string sFileName, PictureBox panel)
		{
			Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile parent " + panel.Handle.ToInt32() + " style child";
			mciSendString(Pcommand, null, 0, 0);
			isOpen = true;
		}

		public void PlayVideo()
		{
			if (isOpen)
			{
				mciSendString("put movie window at 0 0 1280 960", null, 0, 0);
				Pcommand = "play MediaFile";
				mciSendString(Pcommand, null, 0, 0);
			}
		}


		/// <SUMMARY>
		/// Pauses currently playing audio file
		/// </SUMMARY>
		public void Pause()
		{
			Pcommand = "pause MediaFile";
			mciSendString(Pcommand, null, 0, 0);
		}

		/// <SUMMARY>
		/// Returns the current status player: playing, paused, stopped, etc.
		/// </SUMMARY>
		public string Status()
		{
			int i = 128;
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
			mciSendString("status MediaFile mode", stringBuilder, i, 0);
			return stringBuilder.ToString();
		}

		/// <SUMMARY>
		/// Returns the length of the file that is playing
		/// </SUMMARY>
		public string Length()
		{
			int i = 128;
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
			mciSendString("status MediaFile length", stringBuilder, i, 0);
			return stringBuilder.ToString();
		}

		/// <SUMMARY>
		/// Returns the current position of the file that is playing
		/// </SUMMARY>
		public string Position()
		{
			int i = 128;
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(i);
			mciSendString("status MediaFile position", stringBuilder, i, 0);
			return String.IsNullOrEmpty(stringBuilder.ToString()) ? "0" : stringBuilder.ToString();
		}


		public void Seek(int milliseconds)
		{
			mciSendString("seek MediaFile to " + milliseconds, null, 0, 0);
		}
	
		
		
		/// <SUMMARY>
		/// Get/Set Left Volume Factor
		/// </SUMMARY>
		public int LeftVolume
		{
			get
			{
				return 0; //Guess could be used to return Volume level: I don't need it
			}
			set
			{
				mciSendString(string.Concat("setaudio MediaFile left volume to ", value), null, 0, 0);
			}
		}

		/// <SUMMARY>
		/// Get/Set Right Volume Factor
		/// </SUMMARY>
		public int RightVolume
		{
			get
			{
				return 0; //Guess could be used to return Volume level: I don't need it
			}
			set
			{
				mciSendString(string.Concat("setaudio MediaFile right volume to ", value), null, 0, 0);
			}
		}

		/// <SUMMARY>
		/// Get/Set Main Volume Factor
		/// </SUMMARY>
		public int MasterVolume
		{
			get
			{
				return 0; //Guess could be used to return Volume level: I don't need it
			}
			set
			{
				mciSendString(string.Concat("setaudio MediaFile volume to ", value), null, 0, 0);
			}
		}

		/// <SUMMARY>
		/// Get/Set Bass Volume Factor
		/// </SUMMARY>
		public int Bass
		{
			get
			{
				return 0;
			}
			set
			{
				mciSendString(string.Concat("setaudio MediaFile bass to ", value), null, 0, 0);
			}
		}

		/// <SUMMARY>
		/// Get/Set Treble Volume Factor
		/// </SUMMARY>
		public int Treble
		{
			get
			{
				return 0;
			}
			set
			{
				mciSendString(string.Concat("setaudio MediaFile treble to ", value), null, 0, 0);
			}
		}

		public bool getIsOpen()
		{
			return isOpen;
		}
	}
}
